import os
import time
import mysql.connector

conn = mysql.connector.connect(user='root',password='root',database='zzp_work')

print("请用cmd打开此文件")
time.sleep(1)
os.system("cls")

while True:
    print("欢迎使用丰翼超市管理系统")
    print("******************************")
    print("1.登录系统")
    print("2.退出系统")
    print("******************************")
    choose = input("请输入您的选择：")
    if choose == "1":
        os.system("cls")
        while True:
            print("登录")
            username = input("请输入用户名：")
            password = input("请输入密码：")
            if username != "admin" or password!="123456":
                print("用户名或者密码错误")
                time.sleep(1)
                os.system("cls")
            else:
                print("登录成功欢您，"+username)
                time.sleep(2)
                break
        while True:
            os.system("cls")
            print("欢迎使用丰翼超市管理系统       您好！"+username)
            print("******************************")
            print("1.商品管理")
            print("2.会员管理")
            print("3.结账")
            print("4.退出系统")
            print("******************************")
            choose2 = input("请输入您的选择：")
            if choose2 == "1":
                while True:
                    os.system("cls")
                    print("欢迎使用丰翼超市管理系统       您好！"+username)
                    print("******************************")
                    print("1.添加商品")
                    print("2.删除商品")
                    print("3.显示所有商品")
                    print("4.返回上一级")
                    print("******************************")
                    choose3 = input("请输入您的选择：")
                    if choose3 == "1":
                        os.system("cls")
                        print("欢迎使用丰翼超市管理系统       您好！"+username)
                        print("******************************")
                        goodName = input("请输入添加的商品名称：")
                        goodNum = input("请输入添加的商品数量：")
                        goodPrice = input("请输入添加的商品单价：")
                        print("******************************")
                        os.system("cls")
                        while True:
                            print("欢迎使用丰翼超市管理系统       您好！"+username)
                            print("******************************")
                            print("商品名称：",goodName)
                            print("商品数量：", goodNum)
                            print("商品单价：", goodPrice)
                            print("******************************")
                            cursor = conn.cursor()
                            cursor.execute('insert into goods(goodName,goodNum,goodPrice) VALUES(%s,%s,%s)',
                                           (goodName, goodNum, goodPrice))
                            if cursor.rowcount ==1:
                                cursor.close()
                                success = input("添加成功！按q键返回：")
                                if success == "q":
                                    os.system("cls")
                                    break
                                else:
                                    print("请不要输入无关字符")
                            else:
                                cursor.close()
                                err = input("添加失败！")
                                time.sleep(2)
                                os.system("cls")
                                break
                    elif choose3 =="2":
                        os.system("cls")
                        print("欢迎使用丰翼超市管理系统       您好！" + username)
                        print("******************************")
                        find = input("请输入商品名称：")
                        print("******************************")
                        cursor = conn.cursor()
                        cursor.execute('select * from goods where goodName=%s',(find,))
                        result = cursor.fetchall()
                        if(result==[]):
                            err = input("没有找到对应的商品，请重新输入（按q键返回）")
                        else:
                            delet = input("找到此商品，按y键删除，按q键返回：")
                            if(delet == 'y'):
                                cursor = conn.cursor()
                                cursor.execute('delete from goods where goodName=%s',(find,))
                                result = cursor.rowcount
                                if(result ==1):
                                    print("删除成功")
                                elif(result == 0):
                                    print("删除失败")
                            elif(delet == 'q'):
                                pass
                            else:
                                input("请不要输入无关操作")
                                time.sleep(2)
                    elif choose3 == "3":
                        while True:
                            os.system("cls")
                            print("欢迎使用丰翼超市管理系统       您好！" + username)
                            print("******************************")
                            cursor = conn.cursor()
                            cursor.execute('select * from goods ')
                            values = cursor.fetchall()
                            i = 0
                            for val in values:
                                i = i + 1
                                print("%s.%s  库存：%s个，单价：%s,合计：%s元" % (i, val[1], val[2], val[3], val[2] * val[3],))
                            cursor.close()
                            print("******************************")
                            back = input("按q返回：")
                            if(back == "q"):
                                os.system("cls")
                                break
                            else:
                                print("请不要输入无关字符")
                                time.sleep(2)
                                os.system("cls")
                    elif choose3 == "4":
                        print("返回上一级")
                        break
                    else :
                        print("请不要输入无关字符")
                        time.sleep(2)
                        os.system("cls")
            elif choose2 == "2":
                while True:
                    os.system("cls")
                    print("欢迎使用丰翼超市管理系统       您好！" + username)
                    print("******************************")
                    print("1.添加会员")
                    print("2.删除会员")
                    print("3.显示所有会员")
                    print("4.返回上一级")
                    print("******************************")
                    choose4 = input("请输入您的选择：")
                    if(choose4=="1"):
                        while True:
                            os.system("cls")
                            print("欢迎使用丰翼超市管理系统       您好！" + username)
                            print("******************************")
                            cursor = conn.cursor()
                            cursor.execute('select max(userNum) from admin ')
                            values = cursor.fetchall()
                            newNum = time.strftime("%Y%m%d%H%M", time.localtime()) + str(int(values[0][0][-1::5]) + 1).zfill(5)
                            cursor.close()
                            print("会员编号："+newNum)
                            newName = input("请输入会员姓名：")
                            newPoints = input("请输入会员积分：")
                            newPhone = input("请输入会员电话：")
                            cursor = conn.cursor()
                            cursor.execute(
                                'insert into admin(userNum,userName,userPoints,userPhone)VALUES(%s,%s,%s,%s)',
                                (newNum, newName, newPoints, newPhone))
                            success = cursor.rowcount
                            if success==1:
                                while True:
                                    back = input("添加成功，按q键返回上一级：")
                                    if back == "q":
                                        break
                                    else:
                                        print("请不要输入无关字符")
                                        time.sleep(2)
                                        os.system("cls")
                                break
                            if success !=1:
                                pass
                    elif(choose4=="2"):
                        os.system("cls")
                        print("欢迎使用丰翼超市管理系统       您好！" + username)
                        print("******************************")
                        find = input("请输入会员编号：")
                        print("******************************")
                        cursor = conn.cursor()
                        cursor.execute('select * from admin where userNum=%s',(find,))
                        result = cursor.fetchall()
                        if(result==[]):
                            err = input("没有找到对应的会员，请重新输入（按q键返回）")

                        else:
                            delet = input("找到此会员，按y键删除，按q键返回：")
                            if(delet == 'y'):
                                cursor = conn.cursor()
                                cursor.execute('delete from admin where userNum=%s',(find,))
                                result = cursor.rowcount
                                if(result ==1):
                                    print("删除成功")
                                elif(result == 0):
                                    print("删除失败")
                            elif(delet == 'q'):
                                pass
                            else:
                                input("请不要输入无关字符")
                                time.sleep(2)
                                os.system("cls")
                    elif(choose4=="3"):
                        while True:
                            os.system("cls")
                            print("欢迎使用丰翼超市管理系统       您好！" + username)
                            print("******************************")
                            cursor = conn.cursor()
                            cursor.execute('select * from admin ')
                            values = cursor.fetchall()
                            i = 0
                            print("编号\t姓名\t积分\t电话")
                            for val in values:
                                i = i + 1
                                print("%s.%s %s %s %s " % (i, val[1], val[2], val[3], val[4]))
                            cursor.close()
                            print("******************************")
                            back = input("按q返回：")
                            if(back == "q"):
                                os.system("cls")
                                break
                            else:
                                print("请不要输入无关字符")
                                time.sleep(2)
                                os.system("cls")
                    elif(choose4=="4"):
                        print("返回上一级")
                        break
                    else:
                        print("请不要输入无关字符")
                        time.sleep(2)
                        os.system("cls")
            elif choose2 == "3":
                allGoods = []

                while True:
                    os.system("cls")
                    print("请输入您想要购买的商品       您好！" + username)
                    print("******************************")
                    good = input("请输入您想要购买的商品的名称：")
                    cursor = conn.cursor()
                    cursor.execute('select * from goods where goodName=%s', (good,))
                    result = cursor.fetchall()
                    if result == []:
                        print("找不到商品，请重新输入名称")
                    else:
                        num = input("请输入您想要购买的数量：")
                        if result[0][2] < int(num):
                            print("购买数量超过库存数量!无法购买！")
                        else:
                            newResult = []
                            newResult.append(result[0][1])
                            newResult.append(num)
                            newResult.append(result[0][3])
                            newAll = int(num) * result[0][3]
                            newResult.append(newAll)
                            allGoods.append(newResult)
                    ifContinue = input("是否继续购买？（y/n）")
                    if ifContinue == "y":
                        pass
                    elif ifContinue == "n":
                        all = 0
                        os.system("cls")
                        print("结账       您好！" + username)
                        print("******************************")
                        print("商品名称\t数量\t单价\t应收")
                        for i in allGoods:
                            print("%s\t\t%s\t%s\t%s" % (i[0], i[1], i[2], i[3]))
                            all=all+i[3]
                        print("==============================")
                        print("合计：\t\t\t\t%s元"%(all,))
                        print("******************************")
                        back = input("按q返回：")
                        if (back == "q"):
                            os.system("cls")
                            break
                        else:
                            print("请不要输入无关字符")
                            time.sleep(2)
                            os.system("cls")
                    else:
                        print("请不要输入无关字符")
                        time.sleep(2)
                        os.system("cls")
            elif choose2 == "4":
                print("退出系统")
                break
        break

    elif choose == "2":
        print("退出系统")
        break
    else:
        print("请输入1或者2")
        time.sleep(2)
        os.system("cls")
conn.commit()
conn.close()